<template>
  <view class="wrap">
    <u-form class="form" :model="model" :rules="rules" ref="uForm" label-position="left">
      <u-form-item label="name" prop="name" label-width="180">
        <u-input v-model="model.name" placeholder="name"></u-input>
      </u-form-item>
      <u-form-item label="code" prop="code" label-width="180">
        <u-input v-model="model.code" placeholder="code"></u-input>
      </u-form-item>
      <u-form-item label="tenantDomain" prop="tenantDomain" label-width="180">
        <u-input v-model="model.tenantDomain" placeholder="tenantDomain"></u-input>
      </u-form-item>
      <u-form-item label="开始时间" prop="startTime" label-width="180">
        <u-input v-model="model.startTime" placeholder="开始时间"></u-input>
      </u-form-item>
      <u-form-item label="结束时间" prop="endTime" label-width="180">
        <u-input v-model="model.endTime" placeholder="结束时间"></u-input>
      </u-form-item>
      <u-form-item label="状态" prop="status" label-width="180">
        <js-radio v-model="model.status" itemLabel="label" itemValue="value"
                  :items="lockData"></js-radio>
      </u-form-item>
    </u-form>
    <view class="form-footer">
      <u-button class="btn" type="primary" @click="submit">提交</u-button>
      <u-button class="btn" type="default" @click="cancel">关闭</u-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      type: '',
      model: {
        id: 0,
        name: '',
        code: '',
        tenantDomain: '',
        startTime: '',
        endTime: '',
        status: '',
        delFlag: '',
        createBy: '',
        updateBy: '',
        createTime: '',
        updateTime: '',
      },
      lockData: [{
        label: '有效',
        value: '0'
      }, {
        label: '锁定',
        value: '9'
      }],
      rules: {
        name: [
          {required: true, message: 'name不能为空', trigger: 'blur'}
        ],

        code: [
          {required: true, message: 'code不能为空', trigger: 'blur'}
        ],

        tenantDomain: [
          {required: true, message: 'tenantDomain不能为空', trigger: 'blur'}
        ],

        startTime: [
          {required: true, message: '开始时间不能为空', trigger: 'blur'}
        ],

        endTime: [
          {required: true, message: '结束时间不能为空', trigger: 'blur'}
        ],

        status: [
          {required: true, message: 'status不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  onLoad(params) {
    if (params && params.id !== "undefined") {
      this.type = 'edit'
      this.$u.api.unixTenant.getObj(params).then(res => {
        this.model = res.data[0]
      });
    } else {
      this.type = 'add'
      this.model = {}
    }
  },
  methods: {
    submit() {
      this.$refs.uForm.validate(async valid => {
        if (valid) {
          if (this.type === 'edit') {
            await this.$u.api.unixTenant.putObj(this.model)
          } else {
            await this.$u.api.unixTenant.addObj(this.model)
          }
          setTimeout(() => {
            uni.showModal({
              title: '提示',
              content: this.type === 'edit' ? '修改成功' : '添加成功',
              showCancel: false,
              success: function () {
                uni.setStorageSync('refreshList', true);
                uni.navigateTo({
                  url: '/pages/sys/unixTenant/index'
                })
              }
            })
          }, 200)
        } else {
          this.$u.toast('您填写的信息有误，请根据提示修正。')
        }
      });
    },
    cancel() {
      uni.navigateBack();
    }
  }
}
</script>

